package org.duiduo.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class CharUtil {

    private static final String chars = "QAZWSXEDCRFVTGBYHNUJMIKOLP";
    private static final String nums = "0123456789";

    private CharUtil() {
        throw new AssertionError();
    }

    public static String leftPad(String str, int size, char padChar) {
        return StringUtils.leftPad(str, size, padChar);
    }

    public static boolean startsWithAny(String str, String[] arr) {
        return Arrays.asList(arr).contains(str);
    }

    public static Map<String, String> getHttpUrl(String html) {

        Map<String, String> map = new HashMap<>();
        String regex2 = "http[s]{0,1}://.*";
        Pattern pattern2 = Pattern.compile(regex2, Pattern.CASE_INSENSITIVE);
        Matcher matcher2 = pattern2.matcher(html);
        while (matcher2.find()) {
            String s = matcher2.group(0);
            int x = s.indexOf(" ");
            if (x > -1) {
                s = s.substring(0, x);
                if (s.indexOf("');") > -1) {
                    x = s.indexOf("');");
                    s = s.substring(0, x);
                } else if (s.indexOf("')") > -1) {
                    x = s.indexOf("')");
                    s = s.substring(0, x);
                } else if (s.indexOf("\"") > -1) {
                    x = s.indexOf("\"");
                    s = s.substring(0, x);
                }
                map.put(s, s);
            }
        }
        return map;
    }

    /**
     * 去除字符串中的空格、回车、换行符、制表符
     *
     * @param str
     * @return
     */
    public static String trimString(String str) {
        if (StringUtils.isNotBlank(str)) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            str = m.replaceAll("");
        }
        return str;
    }

    /**
     * 获取A-Z随机数
     *
     * @param prefix : 前缀字符
     * @param length : 获取位数
     * @return
     */
    public static String getRandomChar(String prefix, int length) {
        StringBuffer buffer = new StringBuffer(prefix);
        for (int i = 0; i < length; i++) {
            char c = chars.charAt((int) (Math.random() * 26));
            buffer.append(c);
        }
        return buffer.toString();
    }

    /**
     * 获取A-Z随机数
     *
     * @param prefix : 前缀字符
     * @param length : 获取位数
     * @return
     */
    public static String getRandomNum(String prefix, int length) {
        StringBuffer buffer = new StringBuffer(prefix);
        for (int i = 0; i < length; i++) {
            char c = nums.charAt((int) (Math.random() * 10));
            buffer.append(c);
        }
        return buffer.toString();
    }

    public static boolean isTxPwd(String pwd) {
        String regExp = "^[0-9]{6}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(pwd);
        return m.find();
    }

    public static boolean isPwd(String pwd) {
        if (pwd.length() >= 6 && pwd.length() <= 12) {
            String regExp = "^(?![a-zA-z]+$)(?!\\d+$)(?![!@#$%^&*]+$)[a-zA-Z\\d!@#$%^&*]+$";
            Pattern p = Pattern.compile(regExp);
            Matcher m = p.matcher(pwd);
            return m.find();
        } else {
            return false;
        }
    }

    /**
     * 验证手机号-移动
     *
     * @param mobile
     * @return
     */
    public static boolean isMobile_chinaMobile(String mobile) {
        String regExp = "^((134)|(135)|(136)|(137)|(138)|(139)|(147)|(150)|(151)|(152)|(157)|(158)|(159)|(165)|(178)|(182)|(183)|(184)|(187)|(188)|(195)|(198))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(mobile);
        return m.find();
    }

    /**
     * 验证手机号-联通
     *
     * @param mobile
     * @return
     */
    public static boolean isMobile_chinaUnicom(String mobile) {
        String regExp = "^((130)|(131)|(132)|(155)|(156)|(145)|(185)|(186)|(176)|(175)|(170)|(171)|(166))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(mobile);
        return m.find();
    }

    /**
     * 验证手机号-电信
     *
     * @param mobile
     * @return
     */
    public static boolean isMobile_chianTelecom(String mobile) {
        String regExp = "^((133)|(153)|(173)|(177)|(180)|(181)|(189)|(191)|(193)|(199))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(mobile);
        return m.find();
    }

    /**
     * 验证手机号
     *
     * @param mobile
     * @return
     */
    public static boolean isMobile(String mobile) {
        String regExp = "^1[3-9]\\d{9}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(mobile);
        return m.find();
    }

    /**
     * 验证手机号-座机
     *
     * @param mobile
     * @return
     */
    public static boolean isMobile_landLine(String mobile) {
        String regExp = "^0\\d{2,3}-\\d{7,8}|\\(?0\\d{2,3}[)-]?\\d{7,8}|\\(?0\\d{2,3}[)-]*\\d{7,8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(mobile);
        return m.find();
    }

    /**
     * 验证身份证
     *
     * @param idCard
     * @return
     */
    public static boolean isIdCard(String idCard) {
        String regExp = "^[0-9]{17}([0-9]{1}|X|x)$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(idCard);
        return m.find();
    }

    /**
     * 验证银行卡号
     *
     * @param cardNo
     * @return
     */
    public static boolean isCardNo(String cardNo) {
        String regExp = "^[0-9]{15,19}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(cardNo);
        return m.find();
    }

    public static boolean isYuan(String amt) {
        String regExp = "^([0-9]{1,}\\.[0-9]{2}|[0-9]{1,})$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(amt);
        return m.find();
    }

    public static boolean isYuan_2(String amt) {
        String regExp = "^([0-9]{1,}\\.[0-9]{1,2}|[0-9]{1,})$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(amt);
        return m.find();
    }

    public static boolean isMonth(String month) {
        String regExp = "^[0-9]{1,2}:[0-9]{1,2}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(month);
        return m.find();
    }


    public static void main(String[] args) {

//		String month = "12345";
//		 System.out.println(isTxPwd(month));

//		String mobile = "15221477930";
//		System.out.println(isMobile(mobile));
//		String idCard = "430281199011284310";
//		System.out.println(isIdCard(idCard));
// 		String mobile = "021-5339-5088";
// 		if (CharUtil.isMobile_landLine(mobile.toString())){
// 			System.out.println(true);
// 		}else{
// 			System.out.println(false);
// 		}
// 		System.out.println(isNumeric("111a2"));
//		String str = "你好";
//		System.out.println("===========> 测试字符串：" + str);
//        System.out.println("正则判断结果：" + isChineseByREG(str) + " -- " + isChineseByName(str));
//        System.out.println("Unicode判断结果 ：" + isChinese(str));
//        System.out.println("详细判断列表：");
//        char[] ch = str.toCharArray();
//        for (int i = 0; i < ch.length; i++) {
//            char c = ch[i];
//            System.out.println(c + " --> " + (isChinese(c) ? "是" : "否"));
//        }

        String s1 = "java";
        String s2 = "全部是汉字";
        System.out.println("s2:" + isTotalChinese(s2) + ",length:" + s2.length());

    }

    // 根据Unicode编码完美的判断中文汉字和符号
    private static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
            return true;
        }
        return false;
    }

    // 完整的判断中文汉字和符号
    public static boolean isChinese(String strName) {
        char[] ch = strName.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (isChinese(c)) {
                return true;
            }
        }
        return false;
    }

    // 判断是否全部都是中文汉字和符号
    public static boolean isTotalChinese(String strName) {
        char[] ch = strName.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (!isChinese(c)) {
                return false;
            }
        }
        return true;
    }

    // 只能判断部分CJK字符（CJK统一汉字）
    public static boolean isChineseByREG(String str) {
        if (str == null) {
            return false;
        }
        Pattern pattern = Pattern.compile("[\\u4E00-\\u9FBF]+");
        return pattern.matcher(str.trim()).find();
    }

    // 只能判断部分CJK字符（CJK统一汉字）
    public static boolean isChineseByName(String str) {
        if (str == null) {
            return false;
        }
        // 大小写不同：\\p 表示包含，\\P 表示不包含
        // \\p{Cn} 的意思为 Unicode 中未被定义字符的编码，\\P{Cn} 就表示 Unicode中已经被定义字符的编码
        String reg = "\\p{InCJK Unified Ideographs}&&\\P{Cn}";
        Pattern pattern = Pattern.compile(reg);
        return pattern.matcher(str.trim()).find();
    }

    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    /**
     * 判断字符串是否为数字
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();
    }
}
